package com.zd.kids.ui.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.zd.kids.R;
import com.zd.kids.ui.activity.JoinClassComputerActivity;

import androidx.annotation.NonNull;

/**
 * @ClassName: com.zd.kids.ui.view
 * @Description:
 * @Author: hyy
 * @Date: 2020/6/19
 * @Time: 10:21 AM
 */
public class JoinClassPopupWindow extends BottomPopupView {
    private Context context;

    private String mLiveScheme;

    private String mWebPath;

    private String mPsw;

    public JoinClassPopupWindow(@NonNull Context context, String roomId, String psw, String webPath) {
        super(context);
        this.context = context;

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("enterroomnew://live")
                .append("?clientType=3")
                .append("&host=global.talk-cloud.net")
                .append("&serial=")
                .append(roomId)
                .append("&server=global")
                .append("&userrole=2")
                .append("&port=443")
                .append("&password=")
                .append(psw);

        this.mLiveScheme = stringBuilder.toString();
        this.mWebPath = webPath;
        this.mPsw = psw;
    }

    public void setLiveSchemeDatas(String roomId, String psw, String webPath) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("enterroomnew://live")
                .append("?clientType=3")
                .append("&host=global.talk-cloud.net")
                .append("&serial=")
                .append(roomId)
                .append("&server=global")
                .append("&userrole=2")
                .append("&port=443")
                .append("&password=")
                .append(psw);

        this.mLiveScheme = stringBuilder.toString();
        this.mWebPath = webPath;
        this.mPsw = psw;
    }

    @Override
    protected void init() {
        super.init();

        findViewById(R.id.tv_close).setOnClickListener(view -> dismiss());

        findViewById(R.id.ll_phone).setOnClickListener(view -> {
            if (AppUtils.isAppInstalled("com.gapsk.live")) {
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mLiveScheme));
                    context.startActivity(intent);
                } catch (Exception e) {
                    transferToGooglePlay();
                    ToastUtils.showLong(StringUtils.getString(R.string.please_install_live_tips));
                }
            } else {
                transferToGooglePlay();
                ToastUtils.showLong(StringUtils.getString(R.string.please_install_live_tips));
            }
            dismiss();
        });

        findViewById(R.id.ll_computer).setOnClickListener(view -> {
            Intent intent = new Intent(context, JoinClassComputerActivity.class);
            intent.putExtra(JoinClassComputerActivity.PSW_KEY, mPsw);
            intent.putExtra(JoinClassComputerActivity.WEB_PATH_KEY, mWebPath);
            context.startActivity(intent);
            dismiss();
        });

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_select_joinclass_type;
    }


    public void showView() {
        new XPopup.Builder(getContext())
                .dismissOnTouchOutside(false)
                .asCustom(this)
                .show();
    }

    String googlePlay = "com.android.vending";

    private void transferToGooglePlay() {
        try {
            Uri uri = Uri.parse("market://details?id=" + "com.gapsk.live");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(googlePlay)) {
                intent.setPackage(googlePlay);
            }
            if (intent.resolveActivity(context.getPackageManager()) != null) { //可以接收
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else { //没有应用市场，我们通过浏览器跳转到Google Play
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.gapsk.live"));
                //这里存在一个极端情况就是有些用户浏览器也没有，再判断一次
                if (intent.resolveActivity(context.getPackageManager()) != null) { //有浏览器
                    context.startActivity(intent);
                } else { //天哪，这还是智能手机吗？
                    com.zd.kids.common.ToastUtils.getIntence().makeTextShow("请安装google play");
                }
            }

        } catch (Exception e) {
            com.zd.kids.common.ToastUtils.getIntence().makeTextShow("请安装google play");
        }

    }
}
